package com.bestcem.xm.user.grpc.service.converter;

import com.alibaba.fastjson.JSON;
import com.bestcem.bp.xm.user.grpc.v1.services.OrgCondition;
import com.bestcem.xm.common.core.uitls.GrpcDateUtil;
import com.bestcem.xm.user.entity.pojo.OrganizationDO;
import com.bestcem.xm.user.enums.OrgDeliveryAuthTypeEnum;
import com.bestcem.xm.user.service.dto.user.OrgConditionDTO;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * OrgCondition转换类
 *
 * @author ympan <yuming.pan@idiaoyan.com>
 * @version v1.0
 * @date 2022/6/30 15:56
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class OrgConditionConvert {
    /**
     * 公司查询条件msg to 公司查询条件dto
     *
     * @param org 公司查询条件msg
     * @return 公司查询条件dto
     * @author ympan <yuming.pan@idiaoyan.com>
     * @date 15:57 2022/6/30
     */
    public static OrgConditionDTO msg2Dto(OrgCondition org) {
        OrgConditionDTO orgConditionDTO = new OrgConditionDTO();
        orgConditionDTO.setOrgId(org.getId());
        orgConditionDTO.setCode(org.getCode());
        orgConditionDTO.setName(org.getName());
        orgConditionDTO.setContent(org.getContent());
        if (org.hasStatus()) {
            orgConditionDTO.setStatus(org.getStatusValue());
        }
        if (org.hasSource()) {
            orgConditionDTO.setSource(org.getSourceValue());
        }
        orgConditionDTO.setSourceRemark(org.getSourceRemark());
        orgConditionDTO.setRootGroupId(org.getRootGroupId());
        orgConditionDTO.setIndustryId(org.getIndustryId());
        orgConditionDTO.setPackageId(org.getPackageId());
        orgConditionDTO.setFormalPackageId(org.getFormalPackageId());
        if (org.hasFormalExpireTime()) {
            orgConditionDTO.setFormalExpireTime(GrpcDateUtil.getDate(org.getFormalExpireTime()));
        }
        orgConditionDTO.setTrialPackageId(org.getTrialPackageId());
        if (org.hasTrialExpireTime()) {
            orgConditionDTO.setTrialExpireTime(GrpcDateUtil.getDate(org.getTrialExpireTime()));
        }
        orgConditionDTO.setAdminEmail(org.getAdminEmail());
        orgConditionDTO.setAdminPhone(org.getAdminPhone());
        if (org.hasPackageExpireTime()) {
            orgConditionDTO.setPackageExpireTime(GrpcDateUtil.getDate(org.getPackageExpireTime()));
        }
        if (org.hasFreeExpireTime()) {
            orgConditionDTO.setFreeExpireTime(GrpcDateUtil.getDate(org.getFreeExpireTime()));
        }
        orgConditionDTO.setSkin(org.getSkin());
        orgConditionDTO.setPublicKeyFilePath(org.getPubFilePath());
        orgConditionDTO.setPrivateKeyId(org.getPriId());
        if (org.hasDeliverAudit()) {
            orgConditionDTO.setDeliverCheckFlag(org.getDeliverAudit() ? OrgDeliveryAuthTypeEnum.SINGLE_TIME.getType() : OrgDeliveryAuthTypeEnum.LONG_TERM.getType());
        }
        if (org.hasComplete()) {
            orgConditionDTO.setCompleteFlag(org.getComplete() ? 1 : 0);
        }
        orgConditionDTO.setAdminName(org.getAdminName());
        orgConditionDTO.setCorpWallet(org.getWalletId());
        if (org.hasSmsPrice()) {
            orgConditionDTO.setSmsPrice((short) org.getSmsPrice());
        }
        if (org.hasFooter()) {
            orgConditionDTO.setFooter(org.getFooterValue());
        }
        orgConditionDTO.setCheckSmtpId(org.getAuditSmtpId());
        if (org.hasNotifySales()) {
            orgConditionDTO.setNotifySalesFlag(org.getNotifySales() ? 1 : 0);
        }
        if (org.hasInner()) {
            orgConditionDTO.setInnerFlag(org.getInner() ? 1 : 0);
        }
        orgConditionDTO.setPaasConf(org.getPaasConf());
        orgConditionDTO.setDistribution(org.getDistribution());
        if (org.hasDeliveryAuth()) {
            orgConditionDTO.setDeliveryAuthType(org.getDeliveryAuthValue());
        }
        if (!org.getContractUrlsList().isEmpty()) {
            orgConditionDTO.setContractUrls(JSON.parseArray(JSON.toJSONString(org.getContractUrlsList()), String.class).toString());
        }
        orgConditionDTO.setParentId(org.getParentId());
        return orgConditionDTO;
    }

    /**
     * OrgConditionDTO to OrganizationDO
     *
     * @param orgConditionDTO 组织条件DTO
     * @return 组织条件DO
     * @author ympan <yuming.pan@idiaoyan.com>
     * @date 15:59 2022/6/28
     */
    public static OrganizationDO dto2Do(OrgConditionDTO orgConditionDTO) {
        if (Objects.isNull(orgConditionDTO)) {
            return null;
        }
        OrganizationDO organizationDO = new OrganizationDO();
        organizationDO.setOrgId(orgConditionDTO.getOrgId());
        organizationDO.setCode(orgConditionDTO.getCode());
        organizationDO.setName(orgConditionDTO.getName());
        organizationDO.setContent(orgConditionDTO.getContent());
        organizationDO.setStatus(orgConditionDTO.getStatus());
        organizationDO.setSource(orgConditionDTO.getSource());
        organizationDO.setSourceRemark(orgConditionDTO.getSourceRemark());
        organizationDO.setFormalExpireTime(orgConditionDTO.getFormalExpireTime());
        organizationDO.setTrialExpireTime(orgConditionDTO.getTrialExpireTime());
        organizationDO.setAdminEmail(orgConditionDTO.getAdminEmail());
        organizationDO.setAdminPhone(orgConditionDTO.getAdminPhone());
        organizationDO.setPackageExpireTime(orgConditionDTO.getPackageExpireTime());
        organizationDO.setFreeExpireTime(orgConditionDTO.getFreeExpireTime());
        organizationDO.setSkin(orgConditionDTO.getSkin());
        organizationDO.setPublicKeyFilePath(orgConditionDTO.getPublicKeyFilePath());
        organizationDO.setDeliverCheckFlag(orgConditionDTO.getDeliverCheckFlag());
        organizationDO.setCompleteFlag(orgConditionDTO.getCompleteFlag());
        organizationDO.setAdminName(orgConditionDTO.getAdminName());
        organizationDO.setCorpWallet(orgConditionDTO.getCorpWallet());
        organizationDO.setSmsPrice(orgConditionDTO.getSmsPrice());
        organizationDO.setFooter(orgConditionDTO.getFooter());
        organizationDO.setNotifySalesFlag(orgConditionDTO.getNotifySalesFlag());
        organizationDO.setInnerFlag(orgConditionDTO.getInnerFlag());
        organizationDO.setPaasConf(orgConditionDTO.getPaasConf());
        organizationDO.setDistribution(orgConditionDTO.getDistribution());
        organizationDO.setDeliveryAuthType(orgConditionDTO.getDeliveryAuthType());
        organizationDO.setContractUrls(orgConditionDTO.getContractUrls());
        organizationDO.setRootGroupId(orgConditionDTO.getRootGroupId());
        organizationDO.setIndustryId(orgConditionDTO.getIndustryId());
        organizationDO.setPackageId(orgConditionDTO.getPackageId());
        organizationDO.setFormalPackageId(orgConditionDTO.getFormalPackageId());
        organizationDO.setTrialPackageId(orgConditionDTO.getTrialPackageId());
        organizationDO.setPrivateKeyId(orgConditionDTO.getPrivateKeyId());
        organizationDO.setCheckSmtpId(orgConditionDTO.getCheckSmtpId());
        organizationDO.setParentId(orgConditionDTO.getParentId());
        organizationDO.setCreateTime(orgConditionDTO.getCreateTime());
        organizationDO.setCreatorId(orgConditionDTO.getCreatorId());
        organizationDO.setUpdateTime(orgConditionDTO.getUpdateTime());
        organizationDO.setOperatorId(orgConditionDTO.getOperatorId());
        organizationDO.setBanLoginTrialExpiration(orgConditionDTO.getBanLoginTrialExpiryTime());
        return organizationDO;
    }
}
    